package cn.uexpo.lhz.common.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.poi.ss.usermodel.Workbook;

/**
 * POI相关操作
 * @author ldj
 * @date 2020-04-29
 */
public class PoiUtils {

	/**
	 * 生成Excel文件
	 * @param workbook
	 * @param fileName
	 * @return
	 */
	public static String createExcelFile(Workbook workbook, String fileName) {
		OutputStream stream = null;
		File file = null;
		String result = null;
		try {
			String path = "/usr/local/uexpoFile/";
//			String path = "H:/usr/local/uexpoFile/";
			File file1 = new File(path);
			file1.mkdirs();
			file = File.createTempFile(fileName, ".xlsx",file1);
			stream = new FileOutputStream(file.getAbsoluteFile());
			workbook.write(stream);
			result =path+file.getName();
//			result =file.getName();
		}  catch (Exception e) {
			try {
				String path = "D:/userExcel/";
				File file1 = new File(path);
				file1.mkdirs();
				file = File.createTempFile(fileName, ".xlsx",file1);
				stream = new FileOutputStream(file.getAbsoluteFile());
				workbook.write(stream);
				result =path+file.getName();
			} catch (Exception e1) {
				IOUtils.closeQuietly(workbook);
				IOUtils.closeQuietly(stream);
			}
		} finally {
			IOUtils.closeQuietly(workbook);
			IOUtils.closeQuietly(stream);
		}
		return result;
	}
}
